#!/bin/bash

read -p "Enter Mysql Root Password: " MYSQL_PASS

# 安装ppp插件
wget ftp://ftp.samba.org/pub/ppp/ppp-2.4.4.tar.gz 
tar -zxf ppp-2.4.4.tar.gz 
cd ./ppp-2.4.4 
./configure 
cd pppd/plugins/radius/
make
make install 

# 配置ppp radius插件
cd ../../..
cp -R pppd/plugins/radius/etc /usr/local/etc/radiusclient/
sed -i 's/\/etc\/radiusclient\//\/usr\/local\/etc\/radiusclient\//g' /usr/local/etc/radiusclient/dictionary
sed -i '$a\localhost       overgfw' /usr/local/etc/radiusclient/servers 
cat >>/etc/ppp/options.pptpd<<EOF
plugin /usr/local/lib/pppd/2.4.4/radius.so
plugin /usr/local/lib/pppd/2.4.4/radattr.so
radius-config-file /usr/local/etc/radiusclient/radiusclient.conf
EOF
cd ..

# yum安装freeradius2
yum install -y freeradius2 freeradius2-mysql 

# 修改配置文件
sed -i 's/^[ \t]*#[ \t]*sql$/        sql/g' /etc/raddb/sites-enabled/default
sed -i 's/^[ \t]*secret[ \t]*=.*$/  secret = overgfw/g' /etc/raddb/clients.conf
sed -i 's/^[ \t]*password[ \t]*=.*$/  password = "radius_overgfw"/g' /etc/raddb/sql.conf
sed -i 's/^[ \t]*#[ \t]*$INCLUDE sql.conf.*$/  $INCLUDE sql.conf/g' /etc/raddb/radiusd.conf
sed -i -e '160,164s/^[ \t]*#//g' /etc/raddb/users

# 准备数据库配置
mysql -uroot -p${MYSQL_PASS} -e "CREATE USER 'radius'@'%' IDENTIFIED BY 'radius_overgfw'; GRANT USAGE ON *.* TO 'radius'@'%' IDENTIFIED BY 'radius_overgfw' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0; CREATE DATABASE IF NOT EXISTS \`radius\`; GRANT ALL PRIVILEGES ON \`radius\`.* TO 'radius'@'%';" 

mysql -uroot radius -p${MYSQL_PASS} < /etc/raddb/sql/mysql/schema.sql 

mysql -uroot radius -p${MYSQL_PASS} -e "INSERT INTO \`radusergroup\` VALUES ('vpn','vpnusers',1);INSERT INTO \`radgroupcheck\` VALUES (NULL,'vpnusers','Simultaneous-Use',':=','100');INSERT INTO \`radcheck\` VALUES (NULL,'vpn','User-Password',':=','vpn');"

radiusd -X &
service pptpd restart

